//
//  RegistVC.swift
//  旅航ios
//
//  Created by tulip on 2021/5/17.
//

import UIKit
import ZLPhotoBrowser

class RegistVC: CommonVC {

        var isResetPWD=false
        var type:String!
        var PhoneText:RegistInputView!
        var MessageText:VerifyCodeView!
        var pwdText:RegistInputView!
        var RePwdText:RegistInputView!
        var nameText:RegistInputView!
        var sexText:RegistInputView!
        var birthday:RegistInputView!
        var idcard:RegistInputView!
        var address:RegistInputView!
        var xieyi:AgreenBtnView!
        var codeType:String!
        var timer :Timer!
        var leftTime:Int = 60
        var zhengBtn:UIButton!
        var fanBtn:UIButton!
        var zheng_idUrlStr:String!
        var fan_idUrlStr:String!
    
        override func viewDidLoad() {
            super.viewDidLoad()
            if  isResetPWD{
              //  titleLab.text="修改密码"

            }else{
              //  titleLab.text="找回密码"

            }
            head.title.text="修改密码"
            
            // Do any additional setup after loading the view.
            let scrollView=UIScrollView.init(frame: CGRect(x: 0, y: TopSpaceHeight, width: SCREEN_WIDTH, height: SCREEN_HEIGHT-TopSpaceHeight))
            scrollView.bounces=false
            scrollView.showsVerticalScrollIndicator=false
            self.view.addSubview(scrollView)
            
            let line0=UIView.init(frame: CGRect(x: 0, y:  0, width: SCREEN_WIDTH, height: 1))
            line0.backgroundColor=UIColor.init(hexString: "ebebeb")
            scrollView.addSubview(line0)
            
            
            //手机号
           
            PhoneText=RegistInputView.init(frame: CGRect(x: 0, y:  0, width: SCREEN_WIDTH, height: 50))
            PhoneText.textfield.placeholder="请输入手机号码"
            PhoneText.lab.text="手机号"
            scrollView.addSubview(PhoneText)
            
     
            
            //验证码
            MessageText=VerifyCodeView.init(frame: CGRect(x: 0, y: PhoneText.frame.origin.y+PhoneText.frame.size.height, width: SCREEN_WIDTH, height: 50))
            MessageText.lab.text="验证码"
            MessageText.messageBtn.addTarget(self, action: #selector(getVerify), for: .touchUpInside)
            scrollView.addSubview(MessageText)
            
            
            //登录密码
            
            pwdText=RegistInputView.init(frame: CGRect(x: 0, y: MessageText.frame.origin.y+MessageText.frame.size.height, width: SCREEN_WIDTH, height: 50))
            pwdText.textfield.placeholder="请输入新密码"
            pwdText.lab.text="新密码"
            scrollView.addSubview(pwdText)
            
            
            //确认密码
            
            RePwdText=RegistInputView.init(frame: CGRect(x: 0, y: pwdText.frame.origin.y+pwdText.frame.size.height, width: SCREEN_WIDTH, height: 50))
            RePwdText.textfield.placeholder="请再次输入密码"
            RePwdText.lab.text="确认密码"
            scrollView.addSubview(RePwdText)
            
       
            
            
            let idCardLab=UILabel.init(frame: CGRect(x: 15, y: RePwdText.frame.origin.y+RePwdText.frame.size.height+16, width: 60, height: 13))
            idCardLab.text="身份信息"
            idCardLab.textColor=UIColor.init(hexString: "999999")
            idCardLab.font=UIFont.systemFont(ofSize: 13)
            scrollView.addSubview(idCardLab)
            
            let  cardBox=UIView.init(frame: CGRect(x: 0, y: idCardLab.frame.size.height+idCardLab.frame.origin.y+16, width: SCREEN_WIDTH, height: 207))
            cardBox.backgroundColor=UIColor.white
            scrollView.addSubview(cardBox)
            
            let tip=UILabel.init(frame: CGRect(x: SCREEN_WIDTH/2.0-150+12, y: 31, width: 300, height: 12))
            tip.text="拍摄或上传证件原件照片，请确保照片的真实性！"
            tip.textColor=UIColor.init(hexString: "909090")
            tip.font=UIFont.systemFont(ofSize: 12)
            cardBox.addSubview(tip)
            
            let tipImg=UIImageView.init(frame: CGRect(x: tip.frame.origin.x-24, y: 31, width: 14, height: 14))
            tipImg.image=UIImage(named: "tan")
            cardBox.addSubview(tipImg)
            
            zhengBtn=UIButton.init(frame: CGRect(x: 20, y: 65, width: 140, height: 80))
            cardBox.addSubview(zhengBtn)
            zhengBtn.setImage(UIImage(named: "zheng"), for: .normal)
            zhengBtn.tag=1
            zhengBtn.addTarget(self, action: #selector(uploadImg), for: .touchUpInside)
            
            fanBtn=UIButton.init(frame: CGRect(x: SCREEN_WIDTH-160, y: 65, width: 140, height: 80))
            cardBox.addSubview(fanBtn)
            fanBtn.tag=2
            fanBtn.addTarget(self, action: #selector(uploadImg), for: .touchUpInside)
            fanBtn.setImage(UIImage(named: "fan"), for: .normal)
            
            let zhengLab=UILabel.init(frame: CGRect(x: 20, y: 65+80+15, width: 140, height: 12))
            zhengLab.text="上传证件正面"
            zhengLab.textColor=UIColor.init(hexString: "ff3000")
            zhengLab.font=UIFont.systemFont(ofSize: 12)
            cardBox.addSubview(zhengLab)
            
            let fanLab=UILabel.init(frame: CGRect(x: SCREEN_WIDTH-160, y: 65+95, width: 140, height: 12))
            fanLab.text="上传证件反面"
            fanLab.textColor=UIColor.init(hexString: "ff3000")
            fanLab.font=UIFont.systemFont(ofSize: 12)
            cardBox.addSubview(fanLab)
            
            let shenfenLab=UILabel.init(frame: CGRect(x: 15, y: cardBox.frame.origin.y+16+cardBox.frame.size.height, width: 100, height: 13))
            shenfenLab.text="确认身份信息"
            shenfenLab.textColor=UIColor.init(hexString: "999999")
            shenfenLab.font=UIFont.systemFont(ofSize: 13)
            scrollView.addSubview(shenfenLab)
            
            
            //姓名
            
            nameText=RegistInputView.init(frame: CGRect(x: 0, y: shenfenLab.frame.origin.y+shenfenLab.frame.size.height+15, width: SCREEN_WIDTH, height: 50))
            nameText.textfield.placeholder="请输入姓名"
            nameText.lab.text="姓名"
            scrollView.addSubview(nameText)
            
            //性别
            sexText=RegistInputView.init(frame: CGRect(x: 0, y: nameText.frame.origin.y+nameText.frame.size.height, width: SCREEN_WIDTH, height: 50))
            sexText.textfield.placeholder="请输入姓别"
            sexText.lab.text="姓别"
            scrollView.addSubview(sexText)
            //出生日期
            birthday=RegistInputView.init(frame: CGRect(x: 0, y: nameText.frame.origin.y+nameText.frame.size.height, width: SCREEN_WIDTH, height: 50))
            birthday.textfield.placeholder="请输入姓别"
            birthday.lab.text="姓别"
            scrollView.addSubview(birthday)
            
            //身份证
            idcard=RegistInputView.init(frame: CGRect(x: 0, y: birthday.frame.origin.y+birthday.frame.size.height, width: SCREEN_WIDTH, height: 50))
            idcard.textfield.placeholder="请输入身份证号"
            idcard.lab.text="身份证"
            scrollView.addSubview(idcard)
            //地址
            address=RegistInputView.init(frame: CGRect(x: 0, y: idcard.frame.origin.y+idcard.frame.size.height, width: SCREEN_WIDTH, height: 50))
            address.textfield.placeholder="请输入地址"
            address.lab.text="地址"
            scrollView.addSubview(address)
            
            //协议
            xieyi=AgreenBtnView.init(frame: CGRect(x: 0, y: address.frame.origin.y+address.frame.size.height+30, width: SCREEN_WIDTH, height: 20))
            
            scrollView.addSubview(xieyi)
            
            
            //提交
            var signBtn=UIButton.init(frame: CGRect(x: 15, y: xieyi.frame.origin.y+xieyi.frame.size.height+35, width: SCREEN_WIDTH-30, height: 50))
            
            signBtn.setTitle("提交", for: .normal)
            signBtn=(gradientColor(colo1: "ff8e4a", colo2: "ff6635", view: signBtn,width: SCREEN_WIDTH-30) as? UIButton)!
            
            signBtn.titleLabel?.font=UIFont.systemFont(ofSize: 15)
            signBtn.setTitleColor(UIColor.init(hexString: "ffffff"), for: .normal)
            signBtn.layer.cornerRadius=5
            signBtn.layer.masksToBounds=true
            signBtn.addTarget(self, action: #selector(post), for: .touchUpInside)
            scrollView.addSubview(signBtn)
            
            //登录
            let loginBtn=UIButton.init(frame: CGRect(x: SCREEN_WIDTH/2.0-62, y: signBtn.frame.origin.y+50+30, width:124 , height: 13))
            loginBtn.setTitle("已有账户【立即登陆】", for: .normal)
            loginBtn.titleLabel?.font=UIFont.systemFont(ofSize: 12)
            loginBtn.setTitleColor(UIColor.init(hexString: "666666"), for: .normal)
            loginBtn.addTarget(self, action: #selector(gotoLoginvc), for: .touchUpInside)
            scrollView.addSubview(loginBtn)
            
            scrollView.contentSize=CGSize(width: SCREEN_WIDTH, height: loginBtn.frame.origin.y+13+25)
            
            
        }

    @objc func gotoLoginvc(){
       
        let index=(self.navigationController?.viewControllers.firstIndex(of: self))!
        self.navigationController?.popToViewController(self.navigationController?.viewControllers[index - 1] ?? self, animated: true)
    }
    
       
        
        @objc func post(){
            if PhoneText.textfield.text == ""{
                YProgressHUD.showToast("请正确输入手机号")
            }
            else if MessageText.textfield.text == ""{
                YProgressHUD.showToast("请输入验证码")
            }
            else if pwdText.textfield.text == ""{
                YProgressHUD.showToast("请输入密码")
            }
            else if RePwdText.textfield.text == ""{
                YProgressHUD.showToast("请再次输入密码")
            }
            else if RePwdText.textfield.text != pwdText.textfield.text{
                YProgressHUD.showToast("两次输入密码不相同")
            }
           
            else{
                if type == "1"{
                    let dic=["phone":PhoneText.textfield.text,"code":MessageText.textfield.text,"pwd":pwdText.textfield.text,"pwd1":RePwdText.textfield.text]
//                    Network.request(.jjrForgetPwd(dic)) { (JSON) in
//                        YProgressHUD.showToast(JSON["msg"].string)
//                        self.navigationController?.popViewController(animated: true)
//                    } failure: { (er) in
//
//                    }

                }
                
                if type == "2"{
                    let dic=["phone":PhoneText.textfield.text,"code":MessageText.textfield.text,"pwd":pwdText.textfield.text,"pwd1":RePwdText.textfield.text]
//                    Network.request(.fwsForgetPwd(dic)) { (JSON) in
//                        YProgressHUD.showToast(JSON["msg"].string)
//                        self.navigationController?.popViewController(animated: true)
//                    } failure: { (er) in
//
//                    }
                }
            }
        }
        

        @objc func getVerify() {
            
            if PhoneText.textfield.text == "" {
                YProgressHUD.showToast("请输入手机号码")
            }else{
                if type == "1"{
                    codeType="5"
                }
                if type == "2"{
                    codeType="6"
                }
                let dic=["phone":PhoneText.textfield.text,"type":codeType]
//                Network.request(.getMessage(dic)) { (JSON) in
//                    YProgressHUD.showToast(JSON["msg"].string)
//                    if JSON["msg"].string == "发送成功"{
//                        self.MessageBtn.isEnabled=false
//                        self.timer = Timer.scheduledTimer(timeInterval: TimeInterval(1), target: self, selector: #selector(self.tickDown), userInfo: nil, repeats: true)
//                    }
//
//                } failure: { (err) in
//
//                }
            }
            
        }
        
        
        @objc func tickDown() {
            
            //将剩余时间减少1秒
            
            leftTime -= 1
            
            print(leftTime)
            
            //修改button的剩余时间

            MessageText.messageBtn.setTitle(String(leftTime)+"s", for: .normal)
           
            
            //如果剩余时间小于等于0
            
            if leftTime <= 0 {
                
                //取消定时器
                
                timer.invalidate()
                
                MessageText.messageBtn.isEnabled = true
                MessageText.messageBtn.setTitle("获取验证码", for: .normal)
                leftTime = 60
            }
            
        }

        
        
        override func viewWillAppear(_ animated: Bool) {
            self.navigationController?.navigationBar.isHidden=true
        }
    
    
    @objc func uploadImg(sender:UIButton){
        
        var arr:Array<Data>=[]
        let ac = ZLPhotoPreviewSheet()
               ac.selectImageBlock = { [weak self] (images, assets, isOriginal) in
                   // your code
                
                arr.append(images[0].jpegData(compressionQuality: 1)!)
                
                if sender.tag == 1 {
                    
                    Network.request(.upload_identityimg(parametersDict: ["type":"1"], imageData: arr)) { (JSON) in
                        
                            self?.zheng_idUrlStr = JSON["img"].string ?? ""
                        self?.zhengBtn.setImage(images[0], for: .normal)
                        
                    } failure: { (err) in
                        
                    }
                }
                
                if sender.tag == 2 {
                    
                    Network.request(.upload_identityimg(parametersDict: ["type":"2"], imageData: arr)) { (JSON) in
                        
                            self?.fan_idUrlStr = JSON["img"].string ?? ""
                        self?.fanBtn.setImage(images[0], for: .normal)
                    } failure: { (err) in
                        
                    }
                }
                
                
               }
               ac.showPreview(animate: true, sender: self)
        
    }
    
    
    }
